package com.demo.chapter11future.async;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Demo {
	
	//已异步的方式获取价格和折扣
	public static void main(String[] args) {
		AsyncShop shop = new AsyncShop("BestShop");
        long start = System.nanoTime();
        Future<Double> futurePrice = shop.getPrice("myPhone");
        long incocationTime = ((System.nanoTime() - start) / 1_000_000);
        System.out.println("Invocation returned after " + incocationTime + " msecs");
        
        //执行更多任务，比如查询其它商店
        doSome();
        
        try {
        	//在未来某个时间，需要商品价格时，调用get方法
        	System.out.println("Price is " + futurePrice.get(2, TimeUnit.SECONDS));
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }catch (InterruptedException e) {
			
		}catch (TimeoutException e) {
			
		}
        long retrivalTime = ((System.nanoTime() - start) / 1_000_000);
        System.out.println("Price returned after " + retrivalTime + " msecs");
	}
	
	public static void doSome(){
		
	}

}
